﻿namespace ClassLibrary.BusinessLayer.BusinessObjects
{
    using Microsoft.Practices.EnterpriseLibrary.Data;
    using System;
    using System.Data;
    using System.Data.Common;

    public static class Db
    {
        public static Database factory = DatabaseFactory.CreateDatabase();

        public static DbCommand CreateParameters(DbCommand cmd)
        {
            factory.DiscoverParameters(cmd);
            return cmd;
        }

        public static int Delete(DbCommand cmd)
        {
            return factory.ExecuteNonQuery(cmd);
        }

        public static int Delete(string sql)
        {
            return factory.ExecuteNonQuery(CommandType.Text, sql);
        }

        public static string Escape(string s)
        {
            if (string.IsNullOrEmpty(s))
            {
                return "NULL";
            }
            return ("'" + s.Trim().Replace("'", "''") + "'");
        }

        public static string Escape(string s, int maxLength)
        {
            if (string.IsNullOrEmpty(s))
            {
                return "NULL";
            }
            s = s.Trim();
            if (s.Length > maxLength)
            {
                s = s.Substring(0, maxLength - 1);
            }
            return ("'" + s.Trim().Replace("'", "''") + "'");
        }

        public static int ExecuteNonQuery(DbCommand cmd)
        {
            return factory.ExecuteNonQuery(cmd);
        }

        public static IDataReader GetDataReader(DbCommand cmd)
        {
            return factory.ExecuteReader(cmd);
        }

        public static DataRow GetDataRow(DbCommand cmd)
        {
            DataRow row = null;
            DataTable dt = GetDataTable(cmd);
            if (dt.Rows.Count > 0)
            {
                row = dt.Rows[0];
            }
            return row;
        }

        public static DataRow GetDataRow(string sql)
        {
            DataRow row = null;
            DataTable dt = GetDataTable(sql);
            if (dt.Rows.Count > 0)
            {
                row = dt.Rows[0];
            }
            return row;
        }

        public static DataSet GetDataSet(DbCommand cmd)
        {
            return factory.ExecuteDataSet(cmd);
        }

        public static DataSet GetDataSet(string Sql)
        {
            return factory.ExecuteDataSet(CommandType.Text, Sql);
        }

        public static DataTable GetDataTable(DbCommand cmd)
        {
            return GetDataSet(cmd).Tables[0];
        }

        public static DataTable GetDataTable(string sql)
        {
            return GetDataSet(sql).Tables[0];
        }

        public static int GetScalar(DbCommand cmd)
        {
            return (int) factory.ExecuteScalar(cmd);
        }

        public static DbCommand GetStoredProcCommand(string SPName)
        {
            return factory.GetStoredProcCommand(SPName);
        }

        public static int Insert(DbCommand cmd)
        {
            return factory.ExecuteNonQuery(cmd);
        }

        public static int Insert(string sql)
        {
            return factory.ExecuteNonQuery(CommandType.Text, sql);
        }

        public static int Update(DbCommand cmd)
        {
            return factory.ExecuteNonQuery(cmd);
        }

        public static int Update(string sql)
        {
            return factory.ExecuteNonQuery(sql, new object[0]);
        }
    }
}

